<script setup lang="ts">
import {useRoute, useRouter} from "vue-router";
import {useAssetTypeApi, useAssetTypeProperty} from "@/apis/AssetTypeApi";
import {ElMessage, ElMessageBox} from "element-plus";
import MyRow from "@/components/MyRow.vue";
import {ref} from "vue";
import MyList from "@/components/MyList.vue";
import AssetProperty from "@/views/asset/AssetProperty.vue";
import {useAssetDataApi} from "@/apis/AssetDataApi";
import AssetSearchProperty from "@/views/asset/AssetSearchProperty.vue";

const router = useRouter()
const route = useRoute()
const aid = route.query.aid
if (!aid){
  ElMessageBox.alert("页面参数错误","提示",{
    type: "error"
  }).then(() => router.back())
}
const assetTypePropertyApi = useAssetTypeProperty();
const assetTypeApi = useAssetTypeApi()
const assetDataApi = useAssetDataApi()

let searchProperty = ref<Array<any>>([])
let properties = ref<Array<any>>([])
let listProperty = ref<Array<any>>([])
assetTypePropertyApi.list(aid).then((res:any) =>{
  properties.value = res
  searchProperty.value = res.filter((item:any) => item.showSearch == 1)
  listProperty.value = res.filter((item:any) => item.showListItem == 1)
})
let assetType = ref<any>({})
assetTypeApi.id({id: aid}).then((res:any) => assetType.value = res)

let assetData = ref<Array<any>>([])
let page = ref<any>({
  page: 1,
  pageSize: 10,
  total: 0
})
let showAdd = ref(false)
let assetProperties = ref<Array<any>>([])

loadData()
function loadData() {
  const query:any = {assetTypeId: aid}
  if (searchProperty.value.length > 0){
    console.log(searchProperty.value)
    const querys = searchProperty.value.map((item:any) => {
      const propertyValue = item.value;
      if (!propertyValue){
        return
      }
      return {
        propertyId: item.id,
        dataType: item.dataType,
        dataRange: item.value}
    }).filter(item => item!=null)
    query.querys = querys
  }
  assetDataApi.list(query).then((res:any) => {
    assetData.value = res.records
  })
}

const onSave = (row:any) => {
  const ds = assetProperties.value.map((item:any) => {
    return {
      propertyId: item.id,
      type: item.dataType,
      dataValue: item.value
    }
  })
  assetDataApi.save({
    typeId: assetType.value.id,
    dataList: ds
  }).then(() =>{
    showAdd.value = false
    loadData()
  })
}
const onEdit = (row:any) => {
  assetProperties.value = properties.value.map((item:any) => {
    return{
      id: item.id,
      propertyName: item.propertyName,
      dataType: item.dataType,
      value: row[`${item.id}`]
    }
  })
  showAdd.value = true
}
const onShowAdd = () => {
  assetProperties.value = properties.value.map((item:any) => {
    return {
      id: item.id,
      propertyName: item.propertyName,
      dataType: item.dataType
    }
  })
  showAdd.value = true
}
</script>

<template>
  <my-row>
    <el-form>
      <el-row :gutter="3">
        <el-col :sm="6" v-for="item in searchProperty" :key="item.id">
          <el-form-item>
            <asset-search-property v-model="item.value" :options="{type: item.dataType, label: item.propertyName}"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-button type="primary" @click="loadData">搜索</el-button>
    </el-form>
  </my-row>
  <my-row>
    <el-button size="small" plain type="primary" @click="onShowAdd">新增{{assetType.typeName}}</el-button>
  </my-row>
  <my-list :data="assetData" :page="page">
    <el-table-column type="index"/>
    <el-table-column v-for="item in listProperty" :prop="`${item.id}`" :label="`${item.propertyName}`"/>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="small" plain type="primary" @click="onEdit(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </my-list>
  <el-dialog v-model="showAdd" width="1000">
    <el-form label-width="70">
      <el-row :gutter="3">
        <el-col :sm="8" v-for="item in assetProperties">
          <el-form-item :label="item.propertyName">
            <asset-property :options="{type: item.dataType}" v-model="item.value"/>
          </el-form-item>
        </el-col>
        <el-col :sm="24">
          <el-button type="primary" @click="onSave">保存</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<style scoped>

</style>